import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import Main from '../views/Main'
import ItemEdit from '../views/ItemEdit'
import ItemList from '../views/itemList'
import CategoryEdit from '../views/CategoryEdit'
import CategoryList from '../views/CategoryList'
import HeroEdit from '../views/HeroEdit'
import HeroList from '../views/HeroList'
import ArticleEdit from '../views/ArticleEdit'
import ArticleList from '../views/ArticleList'
import AdvertisementEdit from '../views/AdvertisementEdit'
import AdvertisementList from '../views/AdvertisementList'
import AdminUserEdit from '../views/AdminUserEdit'
import AdminUserList from '../views/AdminUserList'
import Login from '../views/Login'
Vue.use(Router)

const router =  new Router({
  routes: [
    {
      path: '/login',
      name: 'Login',
      component: Login,
      meta:{isPublic:true}
    },
     {
      path: '/',
      name: 'main',
      component: Main,
      children:[

        // 分类
        {path:'/categories/create',component:CategoryEdit},
        {path:'/categories/edit/:id',component:CategoryEdit,props:true},
        {path:'/categories/list',component:CategoryList},

        // 物品
        {path:'/items/create',component:ItemEdit},
        {path:'/items/edit/:id',component:ItemEdit,props:true},
        {path:'/items/list',component:ItemList},

        // 英雄
        {path:'/heroes/create',component:HeroEdit},
        {path:'/heroes/edit/:id',component:HeroEdit,props:true},
        {path:'/heroes/list',component:HeroList},

        // 文章
        {path:'/articles/create',component:ArticleEdit},
        {path:'/articles/edit/:id',component:ArticleEdit,props:true},
        {path:'/articles/list',component:ArticleList},
        // 广告
        {path:'/advertisement/create',component:AdvertisementEdit},
        {path:'/advertisement/edit/:id',component:AdvertisementEdit,props:true},
        {path:'/advertisement/list',component:AdvertisementList},

        // 管理员
        {path:'/adminUser/create',component:AdminUserEdit},
        {path:'/adminUser/edit/:id',component:AdminUserEdit,props:true},
        {path:'/adminUser/list',component:AdminUserList}
      ]
    }
  ]
})

router.beforeEach((to,from,next)=>{
  if( !to.meta.isPublic && !sessionStorage.token){
    return next('/login')
  }
  // console.log(to,from,next);
  next()
})

export default router